#include "Card.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
#include "QtCore"
#include"QString"
using namespace std;

Card::Card(char s,int i ,int o,string n)
{
    suit=s;
    id=i;
    owner=o;            // initialize card variables
    num=n;
    trump=false;

}
void Card::updateTrump()
{
    trump=true;        // set card trump to true if card is of trump suit
}
bool Card::operator< (const Card& rhs)
{
   // cout<<this->trump<<"<"<<rhs.trump;

    if(rhs.trump && !(this->trump))
        return true;

        if(!rhs.trump && this->trump)
            return false;



    if(this->suit!=rhs.suit)
        if(getSuitVal(this->suit)<getSuitVal(rhs.suit))
            return true;

    if(getNumValue(this->num)<getNumValue(rhs.num))
        return true;



    return false;
}
bool Card::operator> (const Card& rhs)
{
   // cout<<this->suit<<">"<<rhs.suit;
    if(!rhs.trump && this->trump)
        return true;
    if(rhs.trump && !this->trump)
        return false;

    if(this->suit!=rhs.suit)
        if(getSuitVal(this->suit)>getSuitVal(rhs.suit))
            return true;

    if(getNumValue(this->num)>getNumValue(rhs.num))
        return true;

 return false;
}
Card Card::operator = (const Card& rhs)
{
    this->suit=rhs.suit;
     this->id=rhs.id;
     this->owner=rhs.owner;
     this->num=rhs.num;

     return *this;
}

bool Card::operator== (const Card& rhs)
{
    //cout<<this->suit<<"=="<<rhs.suit;
    if(rhs.suit != this->suit)
            return false;
    else
        if(this->num.compare(rhs.num)!=0)
           return false;
 return true;
}
int Card::getSuitVal(char s)
{
    switch(s)
    {
        case 'D':{return 1;break;}
        case 'C':{return 2;break;}  // return a int value of the suit
        case 'H':{return 3;break;}
        case 'S':{return 4;break;}
    };
    return 0;
}
int Card::getNumValue(string n)
{
    if(isdigit(n[1]))
    {
        int num;
        stringstream(n.substr(0,3)) >>num;
        return num;                         // get int value of card
    }
    else
    {
        switch(n[1])
        {
            case 'J':{return 11;}
            case 'Q':{return 12;}
            case 'K':{return 13;}
            case 'A':{return 14;}
        };
    }
    return 0;
}
int Card::toNumber()
{
    if(isdigit(num[1]))
    {
        int num_temp;
        stringstream(num.substr(0,2)) >>num_temp;
        return num_temp;
    }
    else
    {
        switch(num[1])
        {
            case 'J':{return 11;}
            case 'Q':{return 12;}
            case 'K':{return 13;}
            case 'A':{return 14;}
        };
    }
    return 0;
}

void Card::print()
{
    if(trump)
        qDebug()<<suit<<QString::fromStdString(num)<<"|T|"; //print out card for debugging
    else
         qDebug()<<suit<<QString::fromStdString(num);
   // cout<<"("<<owner<<")";
}
void Card::updateOwner(int o)
{

   owner = o;                   // update card owner
   return;
}
